﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace yaai
{
    public partial class grunka : UserControl
    {

        public event EventHandler thisIsDeleted;
        public event EventHandler addAnotherGrunka;

        public List<WordList> l;

        public int order;

        public grunkaListController parent;


        public grunka(grunkaListController parent, int id)
        {
            this.order = id;
            this.parent = parent;
            this.l = parent.lists;

            InitializeComponent();
            txtWord.Visible = false;
            //cmb_level.Visible = false;
            cmb_list.Visible = false;

            setOrderOnGroupboxText(id);
            
            addListsToComboBox();

            //cmb_level.SelectedIndex = 0;
            cmb_type.SelectedIndex = 0;
        }
        ~grunka()
        {
            if (null != this)
            {
                l = null;
            }
        }


        public int getOrder()
        {
            return order;
        }

        public String getContent()
        {
            String type = cmb_type.SelectedItem.ToString();

            if (type.Equals("word"))
            {
                return txtWord.Text.Trim().ToString();
            }
            else
            {
                WordList ws = cmb_list.SelectedItem as WordList;
                return ws.getRandomWord();
            }
        }

        public simpleGrunka toSimpleGrunka()
        {
            String type = cmb_type.SelectedItem.ToString();
            if (type.Equals("word"))
            {
                return new simpleGrunka(txtWord.Text);
            }
            else
            {
                String SelectedItem = (string)cmb_list.SelectedItem.ToString();
                WordList wl = findMatchingWordList(SelectedItem);

                return new simpleGrunka(wl);
            }
        }

        private WordList findMatchingWordList(String name)
        {
            foreach (WordList w in parent.lists)
            {
                if (name == w.getName())
                {
                    return w;
                }
            }
            return null;
        }


        public String getWordOrListName()
        {
            String type = cmb_type.SelectedItem.ToString();

            if (type.Equals("word"))
            {
                return txtWord.Text.Trim().ToString();
            }
            else
            {
                String SelectedItem = (string)cmb_list.SelectedItem.ToString();

                return "<"  + SelectedItem + ">";
            }
        }


        public void setOrderOnGroupboxText(int order)
        {
            groupBox1.Text = "Grunka " + order;
        }

        private void cmb_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            String selected = cmb_type.SelectedItem.ToString();

            if (selected == "word")
            {
                txtWord.Visible = true;
                txtWord.Enabled = true;

                cmb_list.Visible = false;
                cmb_list.Enabled = false;

                //cmb_level.Visible = false;
                //cmb_level.Enabled = false;

            }
            else if (selected == "list")
            {
                addListsToComboBox();
                txtWord.Visible = false;
                txtWord.Enabled = false;

                cmb_list.Visible = true;
                cmb_list.Enabled = true;

                //cmb_level.Visible = true;
                //cmb_level.Enabled = true;

                cmb_list.SelectedIndex = 0;
                cmb_list.Focus();
            }
        }

        private void addListsToComboBox()
        {
            cmb_list.Items.Clear();
            foreach (WordList list in l)
            {
                cmb_list.Items.Add(list.getName());
            }
            cmb_list.SelectedIndex = 1;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            this.addAnotherGrunka(this, new EventArgs());
        }

        private void btn_remove_Click(object sender, EventArgs e)
        {
            this.thisIsDeleted(this, new EventArgs());
            this.Dispose();
        }

    }
}
